package com.coincalf.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.coincalf.dto.DealRecordDTO;
import com.coincalf.entity.EntrustOrder;
import com.coincalf.framework.http.Response;
import com.coincalf.service.EntrustOrderService;
import com.coincalf.service.TurnoverOrderService;
import com.coincalf.vo.OrderForm;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 订单管理控制类
 * by crow
 * 2018年5月14日18:31:05
 */
@RestController
@RequestMapping("/orderManager")
@Slf4j
@Api(value = "数字货币-订单管理", description = "数字货币-订单管理")
public class OrderManagerController {

    @Autowired
    private EntrustOrderService entrustOrderService;
    @Autowired
    private TurnoverOrderService turnoverOrderService;
    @Autowired
    private CommonController commonController;


    /**
     * 订单管理-委托记录
     *
     * @param form 查询条件
     * @return
     */
    //@PreAuthorize("isAuthenticated()")
    @PostMapping("/entrustOrder")
    @ApiOperation(value = "ORDER-001 委托记录", notes = "委托记录", httpMethod = "POST")
    public Object entrustOrder(
            HttpServletRequest request,
            @RequestBody OrderForm form
    ) {
        Long userId = commonController.getUserId(request);
        EntityWrapper<EntrustOrder> e = new EntityWrapper<>();
        e.eq("user_id", userId);
        if (!StringUtils.isEmpty(form.getSymbol()) && !"0".equals(form.getSymbol())) {
            e.eq("symbol", form.getSymbol());
        }
        if (form.getType() != 0) {
            e.eq("type", form.getType());
        }
        e.orderBy("created", false);
        Page<EntrustOrder> page = new Page<>(form.getCurrent(), form.getSize());
        Page<EntrustOrder> orders = entrustOrderService.selectPage(page, e);
        return Response.ok(orders);
    }

    /**
     * 订单管理-成交记录
     *
     * @param form 查询条件
     * @return
     */
    //@PreAuthorize("isAuthenticated()")
    @PostMapping("/dealOrder")
    @ApiOperation(value = "ORDER-002 成交记录", notes = "成交记录", httpMethod = "POST")
    public Object dealOrder(
            HttpServletRequest request,
            @RequestBody OrderForm form
    ) {
        Long userId = commonController.getUserId(request);
        Page<DealRecordDTO> page = new Page<>(form.getCurrent(), form.getSize());
        Page<DealRecordDTO> pages = turnoverOrderService.selectOrders(page, userId, form.getSymbol(), form.getType());
        return Response.ok(pages);
    }

}
